User list and saving nested attributes workaround

jamesperet 11 gadi atpakaļ
vecāks
revīzija
cec95d4673

+ 4 - 0
app/controllers/agent_controller.rb

@@ -44,4 +44,8 @@ class AgentController < ApplicationController
44 44
       end
45 45
     end
46 46
   end
47
+  
48
+  def agent_list
49
+    @users = User.all
50
+  end
47 51
 end

+ 28 - 2
app/controllers/missions_controller.rb

@@ -1,5 +1,5 @@
1 1
 class MissionsController < ApplicationController
2
-  before_action :set_mission, only: [:show, :edit, :update, :destroy, :launch]
2
+  before_action :set_mission, only: [:show, :edit, :update, :destroy, :launch, :mission_control]
3 3
   
4 4
   before_action :authenticate_user!
5 5
   
@@ -8,17 +8,30 @@ class MissionsController < ApplicationController
8 8
   # GET /missions
9 9
   # GET /missions.json
10 10
   def index
11
+    @user = User.find(current_user.id)
12
+    @missions = @user.mission_agent_invites.where(:status => 'accepted')
13
+  end
14
+  
15
+  def list
11 16
     @missions = Mission.all
12 17
   end
13 18
 
14 19
   # GET /missions/1
15 20
   # GET /missions/1.json
16 21
   def show
22
+    @agent = @mission.mission_agents.where(:user_id => current_user.id)
23
+  end
24
+  
25
+  def mission_control
17 26
   end
18 27
 
19 28
   # GET /missions/new
20 29
   def new
21 30
     @mission = Mission.new
31
+    @mission.mission_agents.build
32
+    @mission.mission_agents.each do |agent|
33
+      agent.mission_agent_steps.build
34
+    end
22 35
   end
23 36
 
24 37
   # GET /missions/1/edit
@@ -30,6 +43,7 @@ class MissionsController < ApplicationController
30 43
   def create
31 44
     @mission = Mission.new(mission_params)
32 45
     @mission.status = 'Initializing'
46
+    @mission.owner = current_user
33 47
     respond_to do |format|
34 48
       if @mission.save
35 49
         format.html { redirect_to @mission, notice: 'Mission was successfully created.' }
@@ -44,6 +58,18 @@ class MissionsController < ApplicationController
44 58
   # PATCH/PUT /missions/1
45 59
   # PATCH/PUT /missions/1.json
46 60
   def update
61
+    params[:mission][:mission_agents_attributes].values.each do |a|
62
+      agent = MissionAgent.find(a[:id])
63
+      agent.description = a[:description]
64
+      if a[:mission_agent_steps_attributes] != nil
65
+        a[:mission_agent_steps_attributes].values.each do |s|
66
+          step = MissionAgentStep.find(s[:id])
67
+          step.description = s[:description]
68
+          step.save
69
+        end
70
+      end
71
+      agent.save
72
+    end
47 73
     respond_to do |format|
48 74
       if @mission.update(mission_params)
49 75
         format.html { redirect_to @mission, notice: 'Mission was successfully updated.' }
@@ -112,6 +138,6 @@ class MissionsController < ApplicationController
112 138
 
113 139
     # Never trust parameters from the scary internet, only allow the white list through.
114 140
     def mission_params
115
-      params.require(:mission).permit(:title, :description, :status, :agent_search_start, :agent_search_end, :mission_agent_attributes)
141
+      params.require(:mission).permit(:title, :description, :status, :agent_search_start, :agent_search_end, :mission_agent_attributes, :mission_agent_step_attributes)
116 142
     end
117 143
 end

+ 1 - 0
app/models/mission.rb

@@ -1,4 +1,5 @@
1 1
 class Mission < ActiveRecord::Base
2
+  belongs_to :owner, :class_name => "User"
2 3
   has_many :mission_agents
3 4
   accepts_nested_attributes_for :mission_agents, allow_destroy:true
4 5
 end

+ 1 - 0
app/models/user.rb

@@ -5,4 +5,5 @@ class User < ActiveRecord::Base
5 5
          :recoverable, :rememberable, :trackable, :validatable
6 6
          
7 7
   has_many :mission_agent_invites
8
+  has_many :missions
8 9
 end

+ 27 - 0
app/views/agent/agent_list.html.erb

@@ -0,0 +1,27 @@
1
+<h1>Listing Agents</h1>
2
+
3
+<table class="table table-bordered">
4
+  <thead>
5
+    <tr>
6
+      <th>Username</th>
7
+      <th>Email</th>
8
+      <th>Date Registered</th>
9
+	 <th>Sign in count</th>
10
+    </tr>
11
+  </thead>
12
+
13
+  <tbody>
14
+    <% @users.each do |user| %>
15
+      <tr>
16
+        <td><%= link_to user.username, agent_profile_path(user) %></td>
17
+        <td><%= user.email %></td>
18
+        <td><%= user.created_at %></td>
19
+	   <td><%= user.sign_in_count%>
20
+      </tr>
21
+    <% end %>
22
+  </tbody>
23
+</table>
24
+
25
+<br>
26
+
27
+<%= link_to 'New Mission', new_mission_path %>

+ 2 - 2
app/views/missions/_form.html.erb

@@ -8,8 +8,8 @@
8 8
     <%= f.input :agent_search_end %>
9 9
     
10 10
     <% @agent = 1 %>
11
-    <%= f.fields_for :mission_agents, MissionAgent.order('created_at ASC').find_all_by_mission_id(@mission) do |builder| %>
12
-    		<%= render 'mission_agent_form', f: builder %>
11
+    <%= f.fields_for :mission_agents, MissionAgent.order('created_at ASC').find_all_by_mission_id(@mission) do |agent_builder| %>
12
+    		<%= render 'mission_agent_form', f: agent_builder %>
13 13
     <%end%>
14 14
     
15 15
   </div>

+ 2 - 2
app/views/missions/_mission_agent_form.html.erb

@@ -5,8 +5,8 @@
5 5
 	<%= f.text_area :description%><br/>
6 6
 	<%= f.check_box :_destroy%>
7 7
 	<%= f.label :_destroy, "Remove Agent"%>
8
-	<%= f.fields_for :mission_agent_steps do |builder| %>
9
-		<%= render 'mission_agent_step_form', f: builder %>
8
+	<%= f.fields_for :mission_agent_steps do |step_builder| %>
9
+		<%= render 'mission_agent_step_form', f: step_builder %>
10 10
 	<%end%>
11 11
 	<% @agent = @agent + 1 %>
12 12
 </fieldset>

+ 13 - 35
app/views/missions/index.html.erb

@@ -1,35 +1,13 @@
1
-<h1>Listing missions</h1>
2
-
3
-<table class="table table-bordered">
4
-  <thead>
5
-    <tr>
6
-      <th>Title</th>
7
-      <th>Description</th>
8
-      <th>Status</th>
9
-      <th>Agent search start</th>
10
-      <th>Agent search end</th>
11
-      <th></th>
12
-      <th></th>
13
-      <th></th>
14
-    </tr>
15
-  </thead>
16
-
17
-  <tbody>
18
-    <% @missions.each do |mission| %>
19
-      <tr>
20
-        <td><%= mission.title %></td>
21
-        <td><%= mission.description %></td>
22
-        <td><%= mission.status %></td>
23
-        <td><%= mission.agent_search_start %></td>
24
-        <td><%= mission.agent_search_end %></td>
25
-        <td><%= link_to 'Show', mission %></td>
26
-        <td><%= link_to 'Edit', edit_mission_path(mission) %></td>
27
-        <td><%= link_to 'Destroy', mission, method: :delete, data: { confirm: 'Are you sure?' } %></td>
28
-      </tr>
29
-    <% end %>
30
-  </tbody>
31
-</table>
32
-
33
-<br>
34
-
35
-<%= link_to 'New Mission', new_mission_path %>
1
+<div class="page-header">
2
+  <h1>Current Missions</h1>
3
+</div>
4
+<ul class="thumbnails">
5
+<% @missions.each do |invite| %>
6
+	<li class="span4">
7
+	    <div class="thumbnail" style= "padding: 5px;">
8
+		      <h3 style= "padding: 5px;"><%= link_to invite.mission_agent.mission.title, mission_path(invite.mission_agent.mission) %></h3>
9
+		      <p style= "padding: 5px; height: 50px;"><%= invite.mission_agent.mission.description %></p>
10
+	    </div>
11
+	</li>
12
+<% end %>
13
+</ul>

+ 35 - 0
app/views/missions/list.html.erb

@@ -0,0 +1,35 @@
1
+<h1>Listing missions</h1>
2
+
3
+<table class="table table-bordered">
4
+  <thead>
5
+    <tr>
6
+      <th>Title</th>
7
+      <th>Description</th>
8
+      <th>Status</th>
9
+      <th>Agent search start</th>
10
+      <th>Agent search end</th>
11
+      <th></th>
12
+      <th></th>
13
+      <th></th>
14
+    </tr>
15
+  </thead>
16
+
17
+  <tbody>
18
+    <% @missions.each do |mission| %>
19
+      <tr>
20
+        <td><%= mission.title %></td>
21
+        <td><%= mission.description %></td>
22
+        <td><%= mission.status %></td>
23
+        <td><%= mission.agent_search_start %></td>
24
+        <td><%= mission.agent_search_end %></td>
25
+        <td><%= link_to 'Show', mission %></td>
26
+        <td><%= link_to 'Edit', edit_mission_path(mission) %></td>
27
+        <td><%= link_to 'Destroy', mission, method: :delete, data: { confirm: 'Are you sure?' } %></td>
28
+      </tr>
29
+    <% end %>
30
+  </tbody>
31
+</table>
32
+
33
+<br>
34
+
35
+<%= link_to 'New Mission', new_mission_path %>

+ 60 - 0
app/views/missions/mission_control.html.erb

@@ -0,0 +1,60 @@
1
+<div class="page-header">
2
+  <h1>
3
+	  Mission Control <small>(<%= @mission.title %>)</small>
4
+	  <% if @mission.status == 'Initializing' %>
5
+	  	<% @status_label = 'label'%>
6
+	  <% end%>
7
+	  <% if @mission.status == 'Launched' %>
8
+	  	<% @status_label = 'label-success'%>
9
+	  <% end%>
10
+  	  <span class="label <%= @status_label %> pull-right" style="margin-top: 2px;"><h4><%= @mission.status %><h4></span>
11
+  </h1>
12
+</div>
13
+
14
+<p id="notice"><%= notice %></p>
15
+
16
+<p>
17
+	<strong>Owner: </strong>
18
+	<%= @mission.owner.username %>
19
+
20
+<p>
21
+  <strong>Description:</strong>
22
+  <%= @mission.description %>
23
+</p>
24
+
25
+<p>
26
+  <strong>Status:</strong>
27
+  <%= @mission.status %>
28
+</p>
29
+
30
+<p>
31
+  <strong>Agent search start:</strong>
32
+  <%= @mission.agent_search_start %>
33
+</p>
34
+
35
+<p>
36
+  <strong>Agent search end:</strong>
37
+  <%= @mission.agent_search_end %>
38
+</p>
39
+
40
+<% @agent_number = 1 %>
41
+<% @mission.mission_agents.each do |agent| %>
42
+	<div class="well well-small">
43
+		<h3>Agent <%= @agent_number %></h3>
44
+		<% if agent.user != nil %>
45
+			Name: <%= agent.user.email %><br>
46
+			Status: 
47
+			<% agent.mission_agent_invites.each do |invite| %>
48
+				<% if invite.user = agent.user %>
49
+					<%= invite.status %>
50
+				<% end %>
51
+			<% end %>
52
+		<% else %>
53
+		Agent not found!
54
+		<% end %>
55
+	</div>
56
+	<% @agent_number = @agent_number + 1 %>
57
+<% end %>
58
+
59
+<%= link_to 'Edit', edit_mission_path(@mission) %> |
60
+<%= link_to 'Back', missions_path %>

+ 24 - 38
app/views/missions/show.html.erb

@@ -1,48 +1,34 @@
1
-<p id="notice"><%= notice %></p>
1
+<div class="page-header">
2
+  <h1>
3
+	  Mission Details <small>(<%= @mission.title %>)</small>
4
+	  <% if @mission.status == 'Initializing' %>
5
+	  	<% @status_label = 'label'%>
6
+	  <% end%>
7
+	  <% if @mission.status == 'Launched' %>
8
+	  	<% @status_label = 'label-success'%>
9
+	  <% end%>
10
+  	  <span class="label <%= @status_label %> pull-right" style="margin-top: 2px;"><h4><%= @mission.status %><h4></span>
11
+  </h1>
12
+</div>
2 13
 
3 14
 <p>
4
-  <strong>Title:</strong>
5
-  <%= @mission.title %>
6
-</p>
7
-
8
-<p>
9
-  <strong>Description:</strong>
10 15
   <%= @mission.description %>
11 16
 </p>
12 17
 
13 18
 <p>
14
-  <strong>Status:</strong>
15
-  <%= @mission.status %>
16
-</p>
17
-
18
-<p>
19
-  <strong>Agent search start:</strong>
20
-  <%= @mission.agent_search_start %>
19
+	<strong>Mission Director: </strong>
20
+	<%= @mission.owner.username %>
21 21
 </p>
22
-
23
-<p>
24
-  <strong>Agent search end:</strong>
25
-  <%= @mission.agent_search_end %>
26
-</p>
27
-
28
-<% @agent_number = 1 %>
29
-<% @mission.mission_agents.each do |agent| %>
30
-	<div class="well well-small">
31
-		<h3>Agent <%= @agent_number %></h3>
32
-		<% if agent.user != nil %>
33
-			Name: <%= agent.user.email %><br>
34
-			Status: 
35
-			<% agent.mission_agent_invites.each do |invite| %>
36
-				<% if invite.user = agent.user %>
37
-					<%= invite.status %>
38
-				<% end %>
39
-			<% end %>
40
-		<% else %>
41
-		Agent not found!
42
-		<% end %>
43
-	</div>
44
-	<% @agent_number = @agent_number + 1 %>
22
+<% @step = 1 %>
23
+<% if @agent.length > 0 %>
24
+	<% @agent.first.mission_agent_steps.each do |step| %>
25
+		<div>
26
+			<h3>Step <%= @step %></h3>
27
+			<%= step.description %>
28
+		<div>
29
+		<% @step = @step + 1 %>
30
+	<% end %>
45 31
 <% end %>
32
+<hr>
46 33
 
47
-<%= link_to 'Edit', edit_mission_path(@mission) %> |
48 34
 <%= link_to 'Back', missions_path %>

+ 4 - 1
config/routes.rb

@@ -4,9 +4,12 @@ AvalancheGame::Application.routes.draw do
4 4
   get "choose_missions" => "agent#choose_mission", :as => :mission_choose
5 5
   get "accept_mission/:id" => "agent#accept_mission", :as => :mission_agent_accept
6 6
   get "denie_mission/:id" => "agent#denie_mission", :as => :mission_agent_denie
7
-  get "agent/:id" => "agent#agent_profile"
7
+  get "agent/:id" => "agent#agent_profile", :as => :agent_profile
8
+  get "agent_list" => "agent#agent_list", :as => :agent_list
8 9
   
9 10
   get "missions/:id/launch" => "missions#launch", :as => :mission_launch
11
+  get "mission_control/:id" => "missions#mission_control", :as => :mission_control
12
+  get "mission_list" => "missions#list", :as => :mission_list
10 13
   resources :missions
11 14
   
12 15
   devise_for :users, :skip => [:sessions, :passwords, :confirmations, :registrations]

+ 5 - 0
db/migrate/20140825202313_add_owner_to_mission.rb

@@ -0,0 +1,5 @@
1
+class AddOwnerToMission < ActiveRecord::Migration
2
+  def change
3
+    add_reference :missions, :owner, index: true
4
+  end
5
+end

+ 4 - 1
db/schema.rb

@@ -11,7 +11,7 @@
11 11
 #
12 12
 # It's strongly recommended that you check this file into your version control system.
13 13
 
14
-ActiveRecord::Schema.define(version: 20140824233356) do
14
+ActiveRecord::Schema.define(version: 20140825202313) do
15 15
 
16 16
   # These are extensions that must be enabled in order to support this database
17 17
   enable_extension "plpgsql"
@@ -75,8 +75,11 @@ ActiveRecord::Schema.define(version: 20140824233356) do
75 75
     t.datetime "agent_search_end"
76 76
     t.datetime "created_at"
77 77
     t.datetime "updated_at"
78
+    t.integer  "owner_id"
78 79
   end
79 80
 
81
+  add_index "missions", ["owner_id"], name: "index_missions_on_owner_id", using: :btree
82
+
80 83
   create_table "users", force: true do |t|
81 84
     t.string   "email",                  default: "", null: false
82 85
     t.string   "encrypted_password",     default: "", null: false